package com.base.system.managedBean;

import java.io.Serializable;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.base.organization.entity.CentralUnit;
import com.base.organization.service.ICentralUnitDAO;
import com.base.security.SecurityConst;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_centralUnitListBean", scope = ManagedBeanScope.REQUEST)
public class CentralUnitListBean implements Serializable {
	private final static Log logger = LogFactory
			.getLog(SecurityUserListBean.class);
	private ICentralUnitDAO centralUnitDAO = SpringBeanHolder
			.getBean("centralUnitDAO");
	@Inject
	private FacesContext context;
	@SaveState
	@Bind(id = "code", attribute = "value")
	private String code;
	@SaveState
	@Bind(id = "unitName", attribute = "value")
	private String unitName;

	@Bind(id = "centralUnitGrid", attribute = "binding")
	private UIDataGrid centralUnitGrid;

	@DataModel(id = "centralUnitGrid")
	private List<CentralUnit> getUnits() {
		if (code == null && unitName == null)
			return centralUnitDAO.getAll();
		String sql = "select cu from CentralUnit cu where cu.name is not null";
		if (null != code && !"".equals(code)) {
			sql += " and unitCode like '%" + code + "%'";
		}
		if (null != unitName && !"".equals(unitName)) {
			sql += " and name like '%" + unitName + "%'";
		}
		return centralUnitDAO.findByHql(sql);

	}

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		logger.debug("Listbean 中的 postBack:" + isPostback);
		if (!isPostback) {
			// data = centralUnitDAO.getAll();
			code = null;
			unitName = null;
		}
	}

	@Action(id = "query")
	public void query() {
		centralUnitGrid.reload();
	}

	@Action(id = "reset")
	public void reset() {
		code = null;
		unitName = null;
		// data = centralUnitDAO.getAll();
		centralUnitGrid.reload();
	}

	private boolean initSelected() {
		Object[] objs = centralUnitGrid.getSelectedValues();
		// System.out.println("取得的选取值是：" + objs);
		if (objs == null || objs.length == 0) {
			alertNoRecordSelected();
			return false;
		}
		context.getExternalContext().getSessionMap().put(
				SecurityConst.CENTRALUNIT_SELECT_ROW_DATA,
				(CentralUnit) objs[0]);
		return true;
	}

	private void alertNoRecordSelected() {
		Browser.execClientScript("window.alert('请选择一条记录！');");
	}

	public void showCentralUnit() {
		if (initSelected()) {
			String url = "/modules/system/showCentralUnit.faces";
			url = context.getExternalContext().getRequestContextPath() + url;
			String script = "addCenterLayoutTab('"
					+ SecurityConst.CENTRALUNIT_SHOW_TAB + "','" + "查看机构"
					+ "', '" + url + "')";
			Browser.execClientScript("window.top", script);
		}
	}

	@Action(id = "editCentralUnit")
	public void editCentralUnit() {
		if (initSelected()) {
			String url = "/modules/system/editCentralUnit.faces";
			url = context.getExternalContext().getRequestContextPath() + url;
			String script = "addCenterLayoutTab('"
					+ SecurityConst.CENTRALUNIT_EDIT_TAB + "','" + "编辑机构"
					+ "', '" + url + "')";
			Browser.execClientScript("window.top", script);
		}
	}

	@Action(id = "deleteCentralUnit")
	public void deleteCentralUnit() {
		Object[] objs = centralUnitGrid.getSelectedValues();
		if (objs == null || objs.length == 0) {
			return;
		}
		centralUnitDAO.delete((CentralUnit) objs[0]);
		// data.remove((CentralUnit) objs[0]);
		centralUnitGrid.reload();
		// Browser.execClientScript("window.alert('删除成功！');");
	}
}
